package junit4;

import static org.junit.Assert.*;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
import org.junit.Test;

public class Junitwr {
	@Test
	public void PrintWriter(){

		String FILENAME = "d:/test1.txt";
        PrintWriter pw = null;
        
        try {
            pw = new PrintWriter(FILENAME);
            for(int i = 0;i<10000000;i++){
            	pw.println("1");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            pw.close();
        }

		System.out.println("write using PrintWriter done");
	}
	@Test 
	public void BufferedWriter(){
		String filename = "d:/test2.txt";
		BufferedWriter bw = null;
        try {

            bw = new BufferedWriter(new FileWriter(filename));
            for(int i = 0;i<10000000;i++){
            	bw.write("1");
            	bw.write("\n");
            }
            bw.flush(); 
            bw.close();

        }catch (Exception e){
            e.printStackTrace();
        }finally{
        	try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
		System.out.println("write using BufferedWriter done");
	}
	/*
	@Test
	public void Scanner(){
		
		 Scanner sc = null;   
	        try {   
					
	        	try {
					sc = new Scanner(new File("d:/test.txt"));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}  
	            while (sc.hasNextLine()) {
	            	System.out.println(sc.nextLine());
	            }
	        } finally {   
	            if (sc != null) {
	                sc.close();   
	            }   
	        }    
	}
	@Test
	public void BufferedReader(){
		long begin = System.currentTimeMillis();
		String FILENAME = "d:/test.txt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(FILENAME));
            while(br.readLine()!=null){
            	System.out.println("2");
            }
        } catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        finally{
            try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        long end = System.currentTimeMillis();
		System.out.println("last "+(end-begin));
		System.out.println("write using BufferedReader done");
	}
	*/
}
